from rest_framework.views import APIView
from user.models import User
from user.views import check_user
from work.models import Work, GoodCate, Good, TaoGood
from rest_framework.response import Response

from work.serializer import WorkSerializer, GoodCateSerializer, GoodSerializer, TaoGoodSerializer


# 注册商店
class RegWork(APIView):
    @check_user
    def post(self,request,payload):
        user_id = payload['id']

        # 前端获取数据
        id_number = request.data.get('id_number')
        mobile = request.data.get('mobile')
        user_info = User.objects.get(id=user_id)
        # 判断是否为商家
        if user_info.id_type == 2:
            if id_number==user_info.id_number and mobile==user_info.mobile:
                return Response({
                    'code':200,'msg':'验证成功,可以注册商店'
                })

        # 前端传入注册商家所需数据
        name = request.data.get('name')
        logo = request.data.get('logo')
        money = request.data.get('money')
        info = request.data.get('info')
        address_detailed = request.data.get('address_detailed')
        longitude = request.data.get('longitude')
        latitude = request.data.get('latitude')

        if not all([name,logo,money,info,address_detailed,longitude,latitude]):
            return Response({
                'code':400,'msg':'请补全注册信息！'
            })

        Work.objects.create(name=name,logo=logo,money=money,info=info,
                address_detailed=address_detailed,longitude=longitude,latitude=latitude)
        return Response({
            'code':200,'msg':'恭喜商家注册成功，赶快上架东西叭！！！'
        })


# 店铺
class WorrkInfoView(APIView):
    @check_user
    def get(self,request,payload):
        user_id = payload['id']

        work_list = []
        work_info = Work.objects.all()
        ser = WorkSerializer(work_info,many=True)
        # for work in work_info:
        #     c = {
        #         'name':work.name,
        #         'logo':work.logo,
        #         'money':work.money,
        #         'info':work.info,
        #         'address':work.address_detailed,
        #         'longitude':work.longitude,
        #         'latitude':work.latitude
        #     }
        #     work_list.append(c)

        return Response({
            'code':200,'msg':'获取成功',
            'work':work_list
        })

class Workinfo(APIView):
    # @check_user
    def put(self,request,pk):
        # user_id = payload['id']
        work = Work.objects.filter(id=pk).first()
        ser = WorkSerializer(instance=work,data=request.data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response({
            'code':200,'msg':'更新成功'
        })

    # @check_user
    def delete(self,request,pk):
        # user_id = payload['id']
        work = Work.objects.filter(id=pk).first()
        work.delete()
        return Response({
            'code':200,'msg':'经高层商讨决定 与此店家不再合作'
        })


# 商品分类
class GoodCateinfo(APIView):
    @check_user
    def get(self,request,payload):
        user_id = payload['id']
        goodcate = GoodCate.objects.all()
        ser = GoodCateSerializer(goodcate,many=True)
        return ser.data


    @check_user
    def post(self,request,payload):
        user_id = payload['id']
        catename = request.data.get('catename')
        create_time = request.data.get('create_time')
        update_time = request.data.get('update_time')
        catetype = request.data.get('catetype')
        work_id = request.data.get('work_id')
        Work.objects.create(catename=catename,create_time=create_time,
                            update_time=update_time,catetype=catetype,work_id=work_id)
        return Response({
            'code':200,'msg':'添加成功'
        })

# 分类下的商品
class GoodsInfo(APIView):
    @check_user
    def get(self,request,payload):
        user_id = payload['id']
        cate_id = request.data.get('ID')  # 前端获取分类id
        goods = Good.objects.filter(id=cate_id).all()  # 分类下的商品
        ser = GoodSerializer(goods,many=True)
        return ser.data

    @check_user
    def post(self,request,payload):
        user_id = payload['id']
        good_name = request.data.get('good_name')
        price = request.data.get('price')
        logo = request.data.get('logo')
        info = request.data.get('info')
        cate_time = request.data.get('cate_time')
        update_time = request.data.get('update_time')
        is_show = request.data.get('is_show')
        cate_id = request.data.get('cate_id')
        work = request.data.get('work')
        workinfo = Work.objects.create(good_name=good_name,price=price,logo=logo,info=info,
                cate_time=cate_time,update_time=update_time,is_show=is_show,cate_id_id=cate_id,work_id=work)

        return Response({
            'code':200,'msg':'添加成功'
        })

    # @check_user
    def delete(self,request,payload,pk):
        # user_id = payload['id']
        good = Good.objects.filter(id=pk).first()
        good.delete()
        return Response({
            'code':200,'msg':'删除商品成功'
        })


# 套餐菜品关系表
class TaoGoodInfo(APIView):
    @check_user
    def get(self,request,payload):
        user_id = payload['id']
        taogood = TaoGood.objects.all()
        ser = TaoGoodSerializer(taogood,many=True)
        return ser.data

    @check_user
    def post(self,request,payload):
        user_id = payload['id']
        price = request.data.get('price')
        create_time = request.data.get('create_time')
        update_time = request.data.get('update_time')
        good = request.data.get('good_id')
        TaoGood.objects.create(price=price,create_time=create_time,update_time=update_time,good_id=good)
        return Response({
            'code':200,'msg':'添加成功'
        })


class TaoGoodView(APIView):
    # @check_user
    def put(self, request, pk):
        # user_id = payload['id']
        work = TaoGood.objects.filter(id=pk).first()
        ser = TaoGoodSerializer(instance=work, data=request.data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response({
            'code': 200, 'msg': '更新成功'
        })

    # @check_user
    def delete(self, request, pk):
        # user_id = payload['id']
        work = TaoGood.objects.filter(id=pk).first()
        work.delete()
        return Response({
            'code': 200, 'msg': '删除成功'
        })


